﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.Tutor
{
    public partial class SessionList : Page
    {
        private readonly ISessionBO _sessionBO = new SessionBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //grvSession.DataSource = RetrieveAllSession();
                //grvSession.DataBind();
            }
        }

        private List<SessionEntity> RetrieveAllSessionById(int id)
        {
            return _sessionBO.RetrieveSessionByCourseId(id);
        }

        protected void ucProgramCourse_OnSelectedCourseChanged()
        {
            grvSession.DataSource = RetrieveAllSessionById(UcProgramCourse1.SelectedCourseValue);
            grvSession.DataBind();
        }

        protected void grvSession_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
                {
                    Response.Redirect(string.Format("EditSession.aspx?id={0}&pid={1}", e.CommandArgument,
                        UcProgramCourse1.SelectedProgramValue));
                }
            }
            if (e.CommandName == "Delete")
            {
                if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
                {
                    int result = _sessionBO.DeleteSessionById(Convert.ToInt32(e.CommandArgument.ToString()));

                    if (result > 0)
                    {
                        Response.Redirect("SessionList.aspx");
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}